// pages/editPersonInfo/editPersonInfo.js
let util = require("../../utils/util")
Page({
  data: {
    name: "",
    nickName: "",
    gender: 0,
    genderArray: ["男", "女"],
    genderIndex: 0,
    age: 0,
    birthday: "",
    constellation: "",
    constellationArray: ["白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座"],
    constellationIndex: 0,
    company: "",
    school: "",
    tel: "",
    email: "",
    intro: "",
    birthdayEndDate: ""
  },
  onLoad: function (options) {
    let birthdayEndDate = util.getDate()
    let that = this
    wx.getStorage({
      key: "person_info",
      success: function(res){
        let data = res.data
        that.setData({
          name: data.name,
          nickName: data.nickName,
          gender: data.gender,
          age: data.age,
          birthday: data.birthday,
          constellation: data.constellation,
          company: data.company,
          school: data.school,
          tel: data.tel,
          email: data.email,
          intro: data.intro,
          birthdayEndDate: birthdayEndDate
        })
      }
    })
  },
  savePersonInfo: function(e) {
    let data = e.detail.value
    wx.setStorage({
      key: "person_info",
      data: {
        name: data.name,
        nickName: data.nickName,
        gender: data.gender,
        age: data.age,
        birthday: data.birthday,
        constellation: data.constellation,
        company: data.company,
        school: data.school,
        tel: data.tel,
        email:data.email,
        intro: data.intro
      },
      success: function(res){
        wx.showToast({
          title: "资料修改成功",
          icon: "success",
          duration: 2000
        })
        setTimeout(function(){
          wx.navigateTo({
            url: "../personInfo/personInfo"
          })
        },2000)
      }
    })
  },
  changeGender: function(e) {
    let genderIndex = e.detail.value
    if (genderIndex != "null") {
      this.setData({
        genderIndex: genderIndex,
        gender: this.data.genderArray[this.data.genderIndex]
      })
    }
  },
  changeBirthday: function(e) {
    let birthday = e.detail.value
    if (birthday != "null") {
      this.setData(
        {birthday: birthday}
      )
    }
  },
  changeConstellation: function(e) {
    let constellationIndex = e.detail.value
    if (constellationIndex != "null") {
      this.setData({
        constellationIndex: constellationIndex,
        constellation: this.data.constellationArray[this.data.constellationIndex]
      })
    }
  }
})